package com.shenyi.mould.rest.controller.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shenyi.core.utils.R;
import com.shenyi.core.utils.StringUtils;
import com.shenyi.core.utils.validator.Assert;
import com.shenyi.wx.annotation.IgnoreAuth;
import com.shenyi.wx.api.IApiUserService;
import com.shenyi.wx.api.ITokenService;
import com.shenyi.wx.entity.LoginInfoDO;
import com.shenyi.wx.entity.UserDO;
import com.shenyi.wx.util.ApiBaseAction;
import com.shenyi.wx.util.ApiUserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author richard.fan
 * @version 1.0
 * @date 2018/11/07
 * @desc 认证
 * @see
 * @since 1.0
 */
@Api(tags = "API登录授权接口")
@RestController
@RequestMapping("/api/auth")
public class ApiAuthController extends ApiBaseAction {

    private Logger logger = Logger.getLogger(getClass());
    @Autowired
    private IApiUserService userService;
    @Autowired
    private ITokenService tokenService;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 登录
     */
    @IgnoreAuth
    @PostMapping("login")
    @ApiOperation(value = "登录接口")
    public R login(String mobile, String password) {
        Assert.isBlank(mobile, "手机号不能为空");
        Assert.isBlank(password, "密码不能为空");

        //用户登录
        long userId = userService.login(mobile, password);

        //生成token
        Map<String, Object> map = tokenService.createToken(userId);

        return R.ok(map);
    }

    /**
     * 登录
     */
    @ApiOperation(value = "登录")
    @IgnoreAuth
    @PostMapping("login_by_weixin")
    public Object loginByWeixin(@RequestBody LoginInfoDO loginInfo) {

        //获取openid
        String requestUrl = ApiUserUtils.getWebAccess(loginInfo.getCode());//通过自定义工具类组合出小程序需要的登录凭证 code
        logger.info("》》》组合token为：" + requestUrl);
        String res = restTemplate.getForObject(requestUrl, String.class);
        logger.info("res==" + res);
        JSONObject sessionData = JSON.parseObject(res);
        String openid = sessionData.getString("openid");
        String session_key = sessionData.getString("session_key");//不知道啥用。
        if (null == sessionData || StringUtils.isNullOrEmpty(openid)) {
            return toResponsFail("登录失败");
        }
        //验证用户信息完整性 防止攻击
//        String sha1 = CommonUtil.getSha1(fullUserInfo.getRawData() + sessionData.getString("session_key"));
//        if (!fullUserInfo.getSignature().equals(sha1)) {
//        	 logger.info("登录失败---验证用户信息完整性"+fullUserInfo.getSignature());
//        	 logger.info("登录失败---验证用户信息完整性 sha1"+sha1);
//            return toResponsFail("登录失败");
//        }
        Date nowTime = new Date();
        UserDO UserDO = userService.queryByOpenId(openid);
        if (null == UserDO) {
            UserDO = new UserDO();
            UserDO.setUsername(loginInfo.getNickName());
            UserDO.setPassword(openid);
            UserDO.setRegister_time(nowTime);
            UserDO.setRegister_ip(this.getClientIp());
            UserDO.setLast_login_ip(UserDO.getRegister_ip());
            UserDO.setLast_login_time(UserDO.getRegister_time());
            UserDO.setWeixin_openid(openid);
            UserDO.setAvatar(loginInfo.getAvatarUrl());
            UserDO.setGender(loginInfo.getGender()); // //性别 0：未知、1：男、2：女
            UserDO.setNickname(loginInfo.getNickName());
            userService.save(UserDO);
        }
        Map<String, Object> tokenMap = tokenService.createToken(UserDO.getUserId());
        String token = MapUtils.getString(tokenMap, "token");

        if (StringUtils.isNullOrEmpty(token)) {
            return toResponsFail("登录失败");
        }
        Map<String, Object> resultObj = new HashMap<String, Object>();
        resultObj.put("openid", openid);
        return toResponsSuccess(resultObj);
    }
}
